package com.hjqjl.module_home.ui.list.recyclerview;

import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.hjqjl.module_home.R;
import com.hjqjl.module_home.databinding.HomeActivityFruitlistBinding;
import com.hjqjl.module_home.model.Fruit;
import com.hjqjl.whapplib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FruitListActivity extends BaseActivity<HomeActivityFruitlistBinding> {
    public static void start(Context context) {
        Intent starter = new Intent(context, FruitListActivity.class);
        context.startActivity(starter);
    }

    private List<Fruit> fruitList = new ArrayList<>();
    private int count = 1;

    @Override
    public void initData() {
        initFruits();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(fruitList);
        pVBinding.homeRvFruitList.setAdapter(adapter);

        pVBinding.homeTvFruitListRvType.setText("垂直LinearLayoutManager");
    }

    @Override
    public void initListener() {
        //切换RecyclerView的布局样式
        pVBinding.homeBtnFruitListChangeRv.setOnClickListener(v -> {
            count++;
            if (count == 1) {
                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
                pVBinding.homeTvFruitListRvType.setText("垂直线性布局LinearLayoutManager");
            } else if (count == 2) {
                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
                pVBinding.homeTvFruitListRvType.setText("水平线性布局LinearLayoutManager");
            } else if (count == 3) {
                GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
                pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
                pVBinding.homeTvFruitListRvType.setText("网格布局GridLayoutManager");
            } else if (count == 4) {
                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
                pVBinding.homeTvFruitListRvType.setText("垂直瀑布流StaggeredGridLayoutManager");
            } else if (count == 5) {
                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL);
                pVBinding.homeRvFruitList.setLayoutManager(layoutManager);
                pVBinding.homeTvFruitListRvType.setText("水平瀑布流StaggeredGridLayoutManager");
                count = 0;
            }
        });
    }

    private void initFruits() {
        for (int i = 0; i < 2; i++) {
            Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.ic_image_default);
            fruitList.add(apple);
            Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.ic_image_default);
            fruitList.add(banana);
            Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.ic_image_default);
            fruitList.add(orange);
            Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.ic_image_default);
            fruitList.add(watermelon);
            Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.ic_image_default);
            fruitList.add(pear);
            Fruit grape = new Fruit("Grape", R.drawable.ic_image_default);
            fruitList.add(grape);
            Fruit pineapple = new Fruit("Pineapple", R.drawable.ic_image_default);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("Strawberry", R.drawable.ic_image_default);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("Cherry", R.drawable.ic_image_default);
            fruitList.add(cherry);
            Fruit mango = new Fruit("Mango", R.drawable.ic_image_default);
            fruitList.add(mango);
        }
    }

    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;  // 产生1-20的随机数
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();
    }
}
